package com.hsy.bibc.modules.common;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.hsy.bibc.R;
import com.jingchen.pulltorefresh.PullToRefreshLayout;

/**
 * Created by hsy on 2016/4/20.
 */
public class PullableListView extends RelativeLayout {
    // 初始状态
    public static final int INIT = 0;
    // 释放刷新
    public static final int RELEASE_TO_REFRESH = 1;
    // 正在刷新
    public static final int REFRESHING = 2;
    // 释放加载
    public static final int RELEASE_TO_LOAD = 3;
    // 正在加载
    public static final int LOADING = 4;
    // 操作完毕
    public static final int DONE = 5;
    public static final int LOAD_FINISH = 6;
    public static final int REFRESH_FINISH = 7;

    // 刷新成功
    public static final int SUCCEED = 0;
    // 刷新失败
    public static final int FAIL = 1;

    private View contentView;
    private ListView listView;
    private PullToRefreshLayout pullToRefreshLayout;

    public PullableListView(Context context) {
        super(context);
        init(context);
    }

    public PullableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PullableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        contentView = inflate(context, R.layout.inc_pullable_list_view, null);
        addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        listView = (ListView) contentView.findViewById(R.id.list);
        pullToRefreshLayout = (PullToRefreshLayout) contentView.findViewById(R.id.pull_to_refresh_layout);

    }

    public void setOnScrollListener(AbsListView.OnScrollListener listener) {
        listView.setOnScrollListener(listener);
    }


    public void setAdapter(ListAdapter adapter) {
        listView.setAdapter(adapter);
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        listView.setOnItemClickListener(listener);
    }

    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
        listView.setOnItemLongClickListener(listener);
    }

    public void setSelection(int position) {
        listView.setSelection(position);
    }

    public int getCurrentTopPosition() {
        int topPosition = listView.getFirstVisiblePosition();
        return topPosition;
    }


    public void setOnRefreshListener(PullToRefreshLayout.OnRefreshListener listener) {
        pullToRefreshLayout.setOnRefreshListener(listener);
    }

    public void loadmoreFinish(int result) {
        pullToRefreshLayout.loadmoreFinish(result);
    }

    public void refreshFinish(int result) {
        pullToRefreshLayout.refreshFinish(result);
    }

    public ListView getListView() {
        return listView;
    }

    public PullToRefreshLayout getPullToRefreshLayout() {
        return pullToRefreshLayout;
    }
}
